/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.life.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.service.CrudService;
import com.fosung.frame.modules.life.dao.OrganizeLifeDao;
import com.fosung.frame.modules.life.entity.Month;
import com.fosung.frame.modules.life.entity.OrganizeLife;
import com.fosung.frame.modules.partyassessment.entity.PartyAssessment;
import com.fosung.frame.modules.partyassessment.service.PartyAssessmentService;
import com.fosung.frame.modules.partyassessmentinfo.entity.PartyAssessmentInfo;
import com.fosung.frame.modules.partyassessmentinfo.service.PartyAssessmentInfoService;
import com.fosung.frame.modules.partysinfo.entity.PartysInfo;
import com.fosung.frame.modules.partysinfo.service.PartysInfoService;
import com.fosung.frame.modules.personassessmen.entity.PersonAssessment;
import com.fosung.frame.modules.personassessmen.service.PersonAssessmentService;
import com.fosung.frame.modules.personassessmentinfo.entity.PersonAssessmentInfo;
import com.fosung.frame.modules.personassessmentinfo.service.PersonAssessmentInfoService;
import com.fosung.frame.modules.score.dao.OrganizePersonDetailDao;
import com.fosung.frame.modules.score.dao.PartyScoreDetailDao;
import com.fosung.frame.modules.score.dao.PersonScoreDetailDao;
import com.fosung.frame.modules.score.entity.OrganizePersonDetail;
import com.fosung.frame.modules.score.entity.PartyScoreDetail;
import com.fosung.frame.modules.score.entity.PersonScoreDetail;
import com.fosung.frame.modules.sys.entity.User;
import com.fosung.frame.modules.sys.utils.UserUtils;

/**
 * 组织生活管理Service
 * 
 * @author zhangfn
 * @version 2017-08-29
 */
@Service
@Transactional(readOnly = true)
public class OrganizeLifeService extends CrudService<OrganizeLifeDao, OrganizeLife> {

	@Autowired
	private OrganizeLifeDao organizeLifeDao;
	@Autowired
	private PartyScoreDetailDao partyScoreDetailDao;
	@Autowired
	private PersonScoreDetailDao personScoreDetailDao;
	@Autowired
	private OrganizePersonDetailDao organizePersonDetailDao;
	@Autowired
	private PartyAssessmentService partyAssessmentService;
	@Autowired
	private PartyAssessmentInfoService partyAssessmentInfoService;
	@Autowired
	private PersonAssessmentService personAssessmentService;
	@Autowired
	private PersonAssessmentInfoService personAssessmentInfoService;
	@Autowired
	private PartysInfoService partysInfoService;

	public OrganizeLife get(String id) {
		return super.get(id);
	}

	public List<OrganizeLife> findList(OrganizeLife organizeLife) {
		return super.findList(organizeLife);
	}

	public Month findListByMonth(OrganizeLife organizeLife) {
		return organizeLifeDao.findListByMonth(organizeLife);
	}

	public Page<OrganizeLife> findPage(Page<OrganizeLife> page, OrganizeLife organizeLife) {
		return super.findPage(page, organizeLife);
	}

	public Page<OrganizeLife> findListByZq(Page<OrganizeLife> page, OrganizeLife organizeLife) {
		organizeLife.setPage(page);
		page.setList(dao.findListByZq(organizeLife));
		return page;

	}
	
	public List<OrganizeLife> findListByZq(OrganizeLife organizeLife) {
		return dao.findListByZq(organizeLife);

	}
	
	

	@Transactional(readOnly = false)
	public void save(OrganizeLife organizeLife) {
		super.save(organizeLife);
	}

	@Transactional(readOnly = false)
	public void upState(OrganizeLife organizeLife) {
		organizeLifeDao.upState(organizeLife);
	}

	@Transactional(readOnly = false)
	public void delete(OrganizeLife organizeLife) {
		super.delete(organizeLife);
	}

	// 组织生活签到
	@Transactional(readOnly = false)
	public void qd(OrganizeLife organizeLife) {
		partyScoreDetailDao.deleteByOrganizeLifeId(organizeLife.getId());
		personScoreDetailDao.deleteByOrganizeLifeId(organizeLife.getId());
		organizePersonDetailDao.deleteByOrganizeLifeId(organizeLife.getId());
		
		organizeLifeDao.upSdNum(organizeLife);
		List<PartyScoreDetail> pList = organizeLife.getPartyScoreDetailList();
		for (int i = 0; i < pList.size(); i++) {
			PartyScoreDetail p = pList.get(i);

			if (p.getIsNewRecord()) {
				p.preInsert();
				partyScoreDetailDao.insert(p);
			} else {
				p.preUpdate();
				partyScoreDetailDao.update(p);
			}

		}
		List<PersonScoreDetail> personList = organizeLife.getPersonScoreDetailList();
		for (int i = 0; i < personList.size(); i++) {
			PersonScoreDetail person = personList.get(i);

			if (person.getIsNewRecord()) {
				person.preInsert();
				personScoreDetailDao.insert(person);
			} else {
				person.preUpdate();
				personScoreDetailDao.update(person);
			}

		}

		List<List<OrganizePersonDetail>> oList = organizeLife.getOrganizePersonDetailList();
		for (int i = 0; i < oList.size(); i++) {
			List<OrganizePersonDetail> oLists = oList.get(i);
			for (int y = 0; y < oLists.size(); y++) {
				OrganizePersonDetail o = oLists.get(y);

				if (o.getIsNewRecord()) {
					o.preInsert();
					organizePersonDetailDao.insert(o);
				} else {
					o.preUpdate();
					organizePersonDetailDao.update(o);
				}

			}

		}
	}

	// 修改组织生活签到
	@Transactional(readOnly = false)
	public void upqd(OrganizeLife organizeLife) {

		partyScoreDetailDao.deleteByOrganizeLifeId(organizeLife.getId());
		personScoreDetailDao.deleteByOrganizeLifeId(organizeLife.getId());
		organizePersonDetailDao.deleteByOrganizeLifeId(organizeLife.getId());

		organizeLifeDao.upSdNum(organizeLife);
		List<PartyScoreDetail> pList = organizeLife.getPartyScoreDetailList();
		for (int i = 0; i < pList.size(); i++) {
			PartyScoreDetail p = pList.get(i);

			if (p.getIsNewRecord()) {
				p.preInsert();
				partyScoreDetailDao.insert(p);
			} else {
				p.preUpdate();
				partyScoreDetailDao.update(p);
			}

		}
		List<PersonScoreDetail> personList = organizeLife.getPersonScoreDetailList();
		for (int i = 0; i < personList.size(); i++) {
			PersonScoreDetail person = personList.get(i);

			if (person.getIsNewRecord()) {
				person.preInsert();
				personScoreDetailDao.insert(person);
			} else {
				person.preUpdate();
				personScoreDetailDao.update(person);
			}

		}
		List<List<OrganizePersonDetail>> oList = organizeLife.getOrganizePersonDetailList();
		for (int i = 0; i < oList.size(); i++) {
			List<OrganizePersonDetail> oLists = oList.get(i);
			for (int y = 0; y < oLists.size(); y++) {
				OrganizePersonDetail o = oLists.get(y);

				if (o.getIsNewRecord()) {
					o.preInsert();
					organizePersonDetailDao.insert(o);
				} else {
					o.preUpdate();
					organizePersonDetailDao.update(o);
				}

			}
		}
	}

	// 新增签到页面查询数据
	public Map getQd(String mode) {
		Map map = new HashMap();
		// 获取组织积分项 这个不对 需要根据当前用户的党组织ID进行查询，关联两个表查询
		// 根据用户的组织机构ID，查询对应的组织类型，根据组织类型查询对应组织类型对应的考核表，然后根据考核表查询积分内容
		PartyAssessment partyAssessment = new PartyAssessment();
		partyAssessment.setState("1");

		// 根据orgid获取typeid
		PartysInfo p = partysInfoService.getByOrgid(UserUtils.getUser().getOffice().getId());
		partyAssessment.setTypeId(p.getTypeId());
		partyAssessment.setState("1");
		List<PartyAssessment> partyAssessmentList = partyAssessmentService.findList(partyAssessment);
		List<PartyAssessmentInfo> partyAssessmentInfoList = new ArrayList<PartyAssessmentInfo>();
		for (int i = 0; i < partyAssessmentList.size(); i++) {
			PartyAssessmentInfo partyAssessmentInfo = new PartyAssessmentInfo();
			partyAssessmentInfo.setPartyAssessmentId(partyAssessmentList.get(i).getId());
			partyAssessmentInfo.setMode(mode);
			List<PartyAssessmentInfo> partyAssessmentInfoList1 = partyAssessmentInfoService
					.findList(partyAssessmentInfo);
			partyAssessmentInfoList.addAll(partyAssessmentInfoList1);
		}

		// 获取党员积分项
		// 根据用户的组织机构ID，查询对应的组织类型，根据组织类型查询对应组织类型对应的考核表，然后根据考核表查询积分内容
		PersonAssessment personAssessment = new PersonAssessment();
		personAssessment.setState("1");
		// 根据orgid获取typeid
		personAssessment.setTypeId(p.getTypeId());
		personAssessment.setState("1");
		List<PersonAssessment> personAssessmentList = personAssessmentService.findList(personAssessment);
		List<PersonAssessmentInfo> personAssessmentInfoList = new ArrayList<PersonAssessmentInfo>();
		for (int i = 0; i < personAssessmentList.size(); i++) {
			PersonAssessmentInfo personAssessmentInfo = new PersonAssessmentInfo();
			personAssessmentInfo.setPersonAssessment(personAssessmentList.get(i).getId());
			personAssessmentInfo.setMode(mode);
			List<PersonAssessmentInfo> personAssessmentList1 = personAssessmentInfoService
					.findList(personAssessmentInfo);
			personAssessmentInfoList.addAll(personAssessmentList1);
		}
		map.put("partyType", p.getPartyType().getName());
		map.put("partyAssessmentInfoList", partyAssessmentInfoList);
		map.put("personAssessmentInfoList", personAssessmentInfoList);

		return map;
	}

	// 签到页面回显数据
	public Map upQd(String id) {
		Map map = new HashMap();
		// 获取组织活动的积分项
		PartyScoreDetail partyScoreDetail = new PartyScoreDetail();
		partyScoreDetail.setOrganizeLifeId(id);
		List<PartyScoreDetail> partyScoreDetailList = partyScoreDetailDao.findList(partyScoreDetail);
		if (partyScoreDetailList.size() > 0) {
			String zzfz = partyScoreDetailList.get(0).getScore();// 组织积分
			map.put("zzfz", zzfz);

		}

		// 获取党员的积分项
		PersonScoreDetail personScoreDetail = new PersonScoreDetail();
		personScoreDetail.setOrganizeLifeId(id);
		List<PersonScoreDetail> personScoreDetailList = personScoreDetailDao.findList(personScoreDetail);
		if (personScoreDetailList.size() > 0) {
			String dyfz = personScoreDetailList.get(0).getScore();// 组织积分
			map.put("dyfz", dyfz);
		}

		// 获取签到党员信息
		OrganizePersonDetail organizePersonDetail = new OrganizePersonDetail();
		organizePersonDetail.setOrganizeLifeId(id);
		// list去重///////////////////////////////////////////////////////////////
		List<OrganizePersonDetail> organizePersonDetailList1 = organizePersonDetailDao.findList(organizePersonDetail);
		Set set = new HashSet();
		List<OrganizePersonDetail> organizePersonDetailList = new ArrayList();

		for (OrganizePersonDetail o : organizePersonDetailList1) {
			if (set.add(o.getPersonId())) {
				organizePersonDetailList.add(o);
			}
		}
		/////////////////////////////////////////////////////////////////////////
		map.put("partyScoreDetailList", partyScoreDetailList);

		map.put("personScoreDetailList", personScoreDetailList);
		map.put("organizePersonDetailList", organizePersonDetailList);

		return map;
	}

	// 党员个人积分申请
	@Transactional(readOnly = false)
	public void personqd(OrganizeLife organizeLife) throws ParseException {

		organizeLife.setColumn25("2");// 党员积分类型 0 组织生活 1党组织 2党员
		organizeLife.setJhstate("3");
		organizeLife.setShstate("2");
		organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());

		String uuid = UUID.randomUUID().toString(); // 获取UUID并转化为String对象
		uuid = uuid.replace("-", "");
		organizeLife.setId(uuid);

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String d = format.format(organizeLife.getStarttime());
		Date sdate = format.parse(d);
		organizeLife.setStarttime(sdate);

		organizeLife.setCreateDate(new Date());
		organizeLife.setUpdateDate(new Date());

		User u = new User();
		u.setId(UserUtils.getUser().getId());
		organizeLife.setCreateBy(u);

		organizeLifeDao.insert(organizeLife);

		List<PersonScoreDetail> personList = organizeLife.getPersonScoreDetailList();
		for (int i = 0; i < personList.size(); i++) {
			PersonScoreDetail person = personList.get(i);

			if (person.getIsNewRecord()) {
				person.preInsert();
				person.setOrganizeLifeId(uuid);
				personScoreDetailDao.insert(person);
			} else {
				person.preUpdate();
				personScoreDetailDao.update(person);
			}

		}
		List<OrganizePersonDetail> oList = organizeLife.getOrganizePersonDetailList1();
		for (int i = 0; i < oList.size(); i++) {

			OrganizePersonDetail o = oList.get(i);

			if (o.getIsNewRecord()) {
				o.preInsert();
				o.setOrganizeLifeId(uuid);
				organizePersonDetailDao.insert(o);
			} else {
				o.preUpdate();
				organizePersonDetailDao.update(o);
			}

		}

	}

	// 修改党员个人积分申请
	@Transactional(readOnly = false)
	public void uppersonqd(OrganizeLife organizeLife) throws ParseException {

		partyScoreDetailDao.deleteByOrganizeLifeId(organizeLife.getId());
		personScoreDetailDao.deleteByOrganizeLifeId(organizeLife.getId());
		organizePersonDetailDao.deleteByOrganizeLifeId(organizeLife.getId());

		organizeLife.setColumn25("2");// 党员积分类型 0 组织生活 1党组织 2党员
		organizeLife.setJhstate("3");
		organizeLife.setShstate("1");
		organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String d = format.format(organizeLife.getStarttime());
		Date sdate = format.parse(d);
		organizeLife.setStarttime(sdate);
		organizeLife.setUpdateDate(new Date());

		User u = new User();
		u.setId(UserUtils.getUser().getId());
		organizeLife.setUpdateBy(u);
		organizeLifeDao.update(organizeLife);

		List<PersonScoreDetail> personList = organizeLife.getPersonScoreDetailList();
		for (int i = 0; i < personList.size(); i++) {
			PersonScoreDetail person = personList.get(i);

			if (person.getIsNewRecord()) {
				person.preInsert();
				person.setOrganizeLifeId(organizeLife.getId());
				personScoreDetailDao.insert(person);
			} else {
				person.preUpdate();
				personScoreDetailDao.update(person);
			}

		}
		List<OrganizePersonDetail> oList = organizeLife.getOrganizePersonDetailList1();
		for (int i = 0; i < oList.size(); i++) {

			OrganizePersonDetail o = oList.get(i);

			if (o.getIsNewRecord()) {
				o.preInsert();
				o.setOrganizeLifeId(organizeLife.getId());
				organizePersonDetailDao.insert(o);
			} else {
				o.preUpdate();
				organizePersonDetailDao.update(o);
			}

		}

	}

	// 组织积分申请
	@Transactional(readOnly = false)
	public void organizeqd(OrganizeLife organizeLife) throws ParseException {

		organizeLife.setColumn25("1");// 党员积分类型 0 组织生活 1党组织 2党员
		organizeLife.setJhstate("3");
		organizeLife.setShstate("2");
		organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());

		String uuid = UUID.randomUUID().toString(); // 获取UUID并转化为String对象
		uuid = uuid.replace("-", "");
		organizeLife.setId(uuid);

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String d = format.format(organizeLife.getStarttime());
		Date sdate = format.parse(d);
		organizeLife.setStarttime(sdate);
		organizeLife.setCreateDate(new Date());
		organizeLife.setUpdateDate(new Date());
		User u = new User();
		u.setId(UserUtils.getUser().getId());
		organizeLife.setCreateBy(u);
		organizeLifeDao.insert(organizeLife);

		List<PartyScoreDetail> pList = organizeLife.getPartyScoreDetailList();
		for (int i = 0; i < pList.size(); i++) {
			PartyScoreDetail p = pList.get(i);

			if (p.getIsNewRecord()) {
				p.preInsert();
				p.setOrganizeLifeId(uuid);
				partyScoreDetailDao.insert(p);
			} else {
				p.preUpdate();
				partyScoreDetailDao.update(p);
			}

		}

	}

	// 修改组织积分申请
	@Transactional(readOnly = false)
	public void uppartyqd(OrganizeLife organizeLife) throws ParseException {

		partyScoreDetailDao.deleteByOrganizeLifeId(organizeLife.getId());
		personScoreDetailDao.deleteByOrganizeLifeId(organizeLife.getId());
		organizePersonDetailDao.deleteByOrganizeLifeId(organizeLife.getId());

		organizeLife.setColumn25("1");// 党员积分类型 0 组织生活 1党组织 2党员
		organizeLife.setJhstate("3");
		organizeLife.setShstate("1");
		organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String d = format.format(organizeLife.getStarttime());
		Date sdate = format.parse(d);
		organizeLife.setStarttime(sdate);
		organizeLife.setUpdateDate(new Date());
		User u = new User();
		u.setId(UserUtils.getUser().getId());
		organizeLife.setUpdateBy(u);
		organizeLifeDao.update(organizeLife);

		List<PartyScoreDetail> pList = organizeLife.getPartyScoreDetailList();
		for (int i = 0; i < pList.size(); i++) {
			PartyScoreDetail p = pList.get(i);

			if (p.getIsNewRecord()) {
				p.preInsert();
				partyScoreDetailDao.insert(p);
			} else {
				p.preUpdate();
				partyScoreDetailDao.update(p);
			}

		}

	}
}